package com.useeinfo.oa.open.api.inspection;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.inspection.biz.InspectionDataBiz;
import com.useeinfo.oa.modules.inspection.entity.InspectionData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/inspectionData")
public class InspectionDataAPI {
    @Autowired
    private InspectionDataBiz inspectionDataBiz;

    /**
     * 获取维保内容列表
     */
    @RequestMapping(value = "/getInspectionDataList")
    public ResultDto getInspectionDataList(@RequestParam(value = "inspectionTechnologyId", required = false) String inspectionTechnologyId,
                                         HttpServletRequest request){
        QueryParam queryParam = new QueryParam(-1,-1);
        queryParam.getSqlMap().put("inspectionTechnologyId",inspectionTechnologyId+"");
        List<InspectionData> inspectionDataList =inspectionDataBiz.findList(queryParam);
        JSONArray array = new JSONArray();
        for (InspectionData inspectionData : inspectionDataList){
            JSONObject json = new JSONObject();
            json.put("name",inspectionData.getName());
            json.put("id",inspectionData.getId());
            if(inspectionData.getDictInfo()!=null){
                json.put("applicationScenariosName",inspectionData.getDictInfo().getPropertyName());
            }else{
                json.put("applicationScenariosName","");
            }
            array.add(json);
        }
        return ResultUtil.success(array);
    }

    /**
     * 获取维保内容详情
     */
    @RequestMapping(value = "/getInspectionDataDetails")
    public ResultDto getInspectionDataDetails(@RequestParam(value = "id", required = false) String id,
                                            HttpServletRequest request){
        InspectionData inspectionData = inspectionDataBiz.findModel(StringConverters.ToLong(id));
        if(inspectionData!=null){
            JSONArray contentArray = new JSONArray();
            if(inspectionData.getContent()!=null){
                contentArray = JSONArray.parseArray(inspectionData.getContent());
            }
            JSONObject inspectionDataJson = inspectionData.toJSONObject2();
            inspectionDataJson.put("contentArray",contentArray);
            return ResultUtil.success(inspectionDataJson);
        }else{
            return ResultUtil.error("无此维保内容信息");
        }

    }
}
